import { _decorator, Component, director, dragonBones, instantiate, Label, Node, Prefab, resources, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { UserInfo } from './UserInfo';
import { AudioMgr } from './utils/AudioMgr';
import { ShopPanel } from './ShopPanel';
import { SignPanel } from './SignPanel';
import { randomNum } from './utils/utils';
import { Adsdk } from './utils/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('Start')
export class Start extends Component {

    @property(Label)
    maxScoreLabel:Label
    @property(Label)
    doubleMaxScoreLabel:Label
    @property(Node)
    setPanel:Node;
    @property(Node)
    shopPanel:Node;
    @property(Node)
    signPanel:Node
    @property(Node)
    advIcon:Node;
    @property(Prefab)
    WindowTip:Prefab;
    @property(Node)
    windowTipRoot:Node;
    @property(Node)
    p1Root:Node;
    @property(Node)
    p2Root:Node;


    static ins:Start=null;
    protected onLoad(): void {
        Start.ins=this;
    }

    protected start(): void {
        Adsdk.instance.showInterstitialAd();

        if (localStorage.getItem("userInfo")) {
            let userInfo = JSON.parse(localStorage.getItem("userInfo"));
            UserInfo.diamondNum = userInfo.diamondNum;
            UserInfo.p1HasSkinArr = userInfo.p1HasSkinArr;
            UserInfo.p2HasSkinArr = userInfo.p2HasSkinArr;
            UserInfo.p1SkinSelect = userInfo.p1SkinSelect;
            UserInfo.p2SkinSelect = userInfo.p2SkinSelect;
            UserInfo.todayAlreadyIsSign=userInfo.todayAlreadyIsSign;
            UserInfo.signContinuousDayNum=userInfo.signContinuousDayNum;
        }
        else {
            //记录一下登录时间
            const now = new Date();
            const date = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            localStorage.setItem('loginTodayTime', date.toLocaleString());
            this.saveGameData();
        }

        if (localStorage.getItem("maxScore")) {
            this.maxScoreLabel.string=JSON.parse(localStorage.getItem("maxScore"));
        }
        if (localStorage.getItem("doubleMaxScore")) {
            this.doubleMaxScoreLabel.string=JSON.parse(localStorage.getItem("doubleMaxScore"));
        }

        const now = new Date();
        const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        let newUserinfo = JSON.parse(localStorage.getItem('userInfo'));
        let loignData = new Date(localStorage.getItem('loginTodayTime'));


        if (localStorage.getItem('loginTodayTime') != nowDate.toLocaleString()) {//如果不是今天

            //重置今天数据
            UserInfo.todayAlreadyIsSign = false;

            this.saveGameData();


            //签到
            if (loignData.getTime()+86400000 == nowDate.getTime()) {//相隔一天
                if (UserInfo.signContinuousDayNum == 6) {//如果已经连续签到7天
                    console.log("已经连续七天签到")
                    UserInfo.signContinuousDayNum = -1;
                    this.saveGameData();
                }
            }
            else{
                UserInfo.signContinuousDayNum = -1;
                this.saveGameData();
            }

            localStorage.setItem('loginTodayTime', nowDate.toLocaleString())
          
        }

            if (UserInfo.isAudioBgm) {
                AudioMgr.ins.play("audio/bgm")
            }
            else{
                AudioMgr.ins.stop();
            }

        if (localStorage.getItem("isAreadlyDoubleModeSeeAdv")) {
            UserInfo.isAreadlyDoubleModeSeeAdv=true;
        }

        if (UserInfo.isAreadlyDoubleModeSeeAdv) {
            this.advIcon.active=false;
        }
        else{
            this.advIcon.active=true;
        }

        this.renewSelectSkinInHome();
        //动态
        tween(this.p1Root)
        .to(randomNum(1,2),{position:v3(-118,240,0)})
        .to(randomNum(1,2),{position:v3(-118,200,0)})
        .union()
        .repeatForever()
        .start()

        tween(this.p2Root)
        .to(randomNum(1,2),{position:v3(123,240,0)})
        .to(randomNum(1,2),{position:v3(123,200,0)})
        .union()
        .repeatForever()
        .start()
        
    }

    onclickDoubleGameStartBtn() {
        AudioMgr.ins.playOneShot("audio/click",0.8);

        if (UserInfo.isAreadlyDoubleModeSeeAdv) {//已经看过广告了，直接进双人模式
            director.loadScene("doubleGame");
        }
        else {//需要看完广告才行
            Adsdk.instance.showVideo(()=>{
                director.loadScene("doubleGame");
                localStorage.setItem("isAreadlyDoubleModeSeeAdv", "true");
            },()=>{

            })
        }

    }
    onclickGameStartBtn() {
        AudioMgr.ins.playOneShot("audio/click",0.8);

        director.loadScene("singleGame");
    }

    onclickSetBtn(){
        Adsdk.instance.showInterstitialAd();

        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.setPanel.active=true;
        this.renewAudioUI();
    }
    onclickSetPanelaudioEffectBtn() {
        if (UserInfo.isAudioEffect) {
            UserInfo.isAudioEffect = false;
        }
        else {
            UserInfo.isAudioEffect = true;
        }

        this.renewAudioUI();
    }
    onclickSetPanelmusicBtn() {
        if (UserInfo.isAudioBgm) {
            UserInfo.isAudioBgm = false;
            AudioMgr.ins.stop();
        }
        else {
            UserInfo.isAudioBgm = true;
            AudioMgr.ins.play("audio/bgm");
        }
        this.renewAudioUI();

    }


    renewAudioUI() {
        if (UserInfo.isAudioBgm) {
            resources.load("audioImg/musicOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
        else {
            resources.load("audioImg/musicOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("bgmBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }

        if (UserInfo.isAudioEffect) {
            resources.load("audioImg/effectOn/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
        else {
            resources.load("audioImg/effectOff/spriteFrame", SpriteFrame, (err, res) => {
                this.setPanel.getChildByName("effectBtn").getChildByName("sp").getComponent(Sprite).spriteFrame = res;

            })
        }
    }

    onclickSetPanelCloseBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.setPanel.active=false;
    }
    onclickShopPanelCloseBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.shopPanel.active=false;
    }
    onclickSignPanelCloseBtn(){
        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.signPanel.active=false;
    }

    onclickP1Shop(){
        Adsdk.instance.showInterstitialAd();

        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.shopPanel.active=true;
        this.shopPanel.getComponent(ShopPanel).init(0);
    }
    onclickP2Shop(){
        Adsdk.instance.showInterstitialAd();

        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.shopPanel.active=true;
        this.shopPanel.getComponent(ShopPanel).init(1);
    }
  
    onclickSignBtn(){
        Adsdk.instance.showInterstitialAd();

        AudioMgr.ins.playOneShot("audio/click",0.8);

        this.signPanel.active=true;
        this.signPanel.getComponent(SignPanel).init();
    }


    saveGameData() {
        let data = {
            p1HasSkinArr:UserInfo.p1HasSkinArr,
            p2HasSkinArr:UserInfo.p2HasSkinArr,
            p1SkinSelect:UserInfo.p1SkinSelect,
            p2SkinSelect:UserInfo.p2SkinSelect,
            diamondNum:UserInfo.diamondNum,
            signContinuousDayNum:UserInfo.signContinuousDayNum,
            todayAlreadyIsSign:UserInfo.todayAlreadyIsSign
        }

        localStorage.setItem("userInfo",JSON.stringify(data));
    }

    createWindowTip(str){
        let pre=instantiate(this.WindowTip);
        pre.setParent(this.windowTipRoot)
        pre.setPosition(0,-300)
        pre.getChildByName("Label").getComponent(Label).string=str;
        tween(pre)
        .by(1,{position:v3(0,200,0)})
        .call(()=>{
            pre.destroy();
        })
        .start()
    }

    onclickShareBtn() {
        console.log('share')
        //@ts-ignore
        let wx=window["tt"]||window["wx"];
        wx.shareAppMessage(function () {
            return {
                title: '你有玩过这款超好玩的双人跳一跳游戏吗?',
                imageUrl: '',
            }
        })
    }

    renewSelectSkinInHome() {
        //p1更换皮肤
        switch (UserInfo.p1SkinSelect) {
            case 0:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
            case 1:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "playerGold"
                break;
            case 2:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "huahuaGold"
                break;
            case 3:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "jiewu"
                break;
            case 4:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "gongzhuDark"
                break;
            case 5:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "hongpi"
                break;

            default:
                this.p1Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
        }
        //p2更换皮肤
        switch (UserInfo.p2SkinSelect) {
            case 0:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
            case 1:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "playerGold"
                break;
            case 2:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "huahuaGold"
                break;
            case 3:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "jiewu"
                break;
            case 4:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "gongzhuDark"
                break;
            case 5:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "hongpi"
                break;

            default:
                this.p2Root.getChildByName("sp").getComponent(dragonBones.ArmatureDisplay).armatureName = "player"
                break;
        }
    }





}


